<?php
declare (strict_types = 1);
namespace app\admin\model\site\navi;

use app\admin\model\content\agreement\Agreement;
use app\common\model\BaseModel;
use app\school\model\school\School;

/**
 * 定义底部导航数据模型
 */
class NaviBottom extends BaseModel
{
    /**
     * 表名
     * @var string
     */
    protected $name = 'site_navi_bottom';

    /**
     * 设置自动写入时间
     * @var boolean
     */
    protected $autoWriteTimestamp = true;

    /**
     * 时间显示格式
     * @var string
     */
    protected $dateFormat = 'Y-m-d H:i';

    /**
     * 定义表主键
     * @var string
     */
    protected $pk = 'id';

    protected $append = ['data_type_text', 'content_text'];
    // 全局查询范围
    protected $globalScope = ['mhm'];

    /**
     * 新增数据事件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-14
     * @return   string
     */
    public static function onBeforeInsert($navi)
    {
        // 机构
        $navi->mhm_id = $navi->getAttr('mhm_id') ?: request()->mhm_id;
    }

    /**
     * 限制机构条件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-16
     * @param    [type]                         $query [description]
     * @return   [type]                                [description]
     */
    public function scopeMhm($query)
    {
        // 机构
        $query->where('mhm_id', request()->mhm_id);
    }

    /**
     * 机构
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-02
     * @return   [type]                         [description]
     */
    public function school()
    {
        return $this->belongsTo(School::class, 'mhm_id')->bind(['school_name' => 'title']);
    }

    /**
     * 获取内容文字
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-05
     * @param    [type]                         $value [description]
     * @param    [type]                         $data  [description]
     * @return   [type]                                [description]
     */
    public function getContentTextAttr($value, $data)
    {
        if ($data['pid'] == 0) {
            return '-';
        }

        $content = '';

        switch ($data['data_type']) {
            case 0:
                // 富文本编辑器编辑的内容,列表暂不显示
                $content = '';
                break;
            case 2:
                // 协议
                $content = app(Agreement::class)->where('id', $data['content'])->value('title');
                break;
            default:
                // 其他
                $content = $data['content'];
                break;
        }

        return $content;
    }
    /**
     * 获取数据类型文字
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-05
     * @param    [type]                         $value [description]
     * @param    [type]                         $data  [description]
     * @return   [type]                                [description]
     */
    public function getDataTypeTextAttr($value, $data)
    {
        if ($data['pid'] == 0) {
            return '-';
        }
        $text = $this->getDataTypeInfo();

        return $text[$data['data_type']] ?? '-';
    }

    public function getDataTypeInfo()
    {
        $types = [
            0 => '文本内容',
            1 => '链接',
        ];

        if (request()->is_platform) {
            $types[2] = '协议';
        }

        return $types;
    }

    /**
     * 获取数据类型选项数据
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-04
     * @return   [type]                         [description]
     */
    public function getDataTypeOptions()
    {
        $options = [];

        $status = $this->getDataTypeInfo();
        foreach ($status as $key => $value) {
            $options[] = ['title' => $value, 'value' => $key];
        }

        return $options;
    }

}
